from django.shortcuts import render
from booktest.models import BookInfo, HeroInfo
from django.template import RequestContext

# Create your views here.

def index(request):
    return render(request, 'booktest/index.html')

def get_all(request):
    allbooks = BookInfo.objects.all()
    print(allbooks[0].btitle)   # test
    context = RequestContext(request, {'allbooks':allbooks})
    return render(request, 'booktest/get_all.html', context)

def get_book_isDel_filter(request):
    books_isDel =BookInfo.objects.filter(isDelete=False)
    context = RequestContext(request, {'books_isDel':books_isDel})
    return render(request, 'booktest/get_book_isDel_filter.html', context)

def get_books_containsWord(request):
    contain_word = '画'
    books_paint =BookInfo.objects.filter(btitle__contains=contain_word)
    context = RequestContext(request, {'books_paint':books_paint})
    return render(request, 'booktest/get_books_containsWord.html', context)

def get_books_not_containsWord(request):
    excontain_word = '画'
    books_nopaint =BookInfo.objects.exclude(btitle__contains=excontain_word)
    context = RequestContext(request, {'books_nopaint':books_nopaint})
    return render(request, 'booktest/get_books_not_containsWord.html', context)

def get_books_startWord(request):
    start_word = '小'
    books_startxiao =BookInfo.objects.filter(btitle__startswith=start_word)
    context = RequestContext(request, {'books_startxiao':books_startxiao})
    return render(request, 'booktest/get_books_startWord.html', context)

# 这里会报错, 因为所有字段在数据库已经设置为非空了
def get_books_title_notnull(request):
    books_title_notnull = BookInfo.objects.filter(btitle__isnotnull=True)
    context = RequestContext(request, {'books_title_notnull':books_title_notnull})
    return render(request, 'booktest/get_books_title_notnull.html', context)

def get_books_parts(request):
    books_parts = BookInfo.objects.filter(pk__in=[1,2,3,4,5])
    context = RequestContext(request, {'books_parts':books_parts})
    return render(request, 'booktest/get_books_parts.html', context)

def get_books_commet_gt(request):
    books_gt10 = BookInfo.objects.filter(bcommet__gt=20)
    context = RequestContext(request, {'books_gt10':books_gt10})
    return render(request, 'booktest/get_books_commet_gt.html', context)

def get_books_hero_containWord(request):
    contain_word = '鲁'
    books_hero_containLu =BookInfo.objects.filter(heroinfo__hname__contains=contain_word)
    print(books_hero_containLu[0].btitle)
    context = RequestContext(request, {'books_hero_containLu':books_hero_containLu})
    return render(request, 'booktest/get_books_hero_containWord.html', context)


# 聚合
from django.db.models import Max,Min,Sum,Avg,Count
def print_books_aggregate(request):
    max_commet = BookInfo.objects.aggregate(Max('bcommet'))
    min_commet = BookInfo.objects.aggregate(Min('bcommet'))
    sum_commet = BookInfo.objects.aggregate(Sum('bcommet'))
    avg_commet = BookInfo.objects.aggregate(Avg('bcommet'))
    count_commet = BookInfo.objects.aggregate(Count('bcommet'))
    print(max_commet)       # dict
    print(min_commet)       # dict
    print(sum_commet)       # dict
    print(avg_commet)       # dict
    books_gt10 = BookInfo.objects.filter(bcommet__gt=20)
    count = books_gt10.count()
    print(count)
    return render(request, 'booktest/index.html')

# 不同字段的比较
from django.db.models import F
def get_books_read_gt_commet(request):
    booklist = BookInfo.objects.filter(bread__gt=F('bcommet'))
    context = RequestContext(request, {'booklist':booklist})
    return render(request, 'booktest/get_books_read_gt_commet.html', context)

# OR
from django.db.models import Q
def get_books_OR(request):
    booklist = BookInfo.objects.filter(Q(pk__lt=4) | Q(bcommet__gt=30))
    context = RequestContext(request, {'booklist':booklist})
    return render(request, 'booktest/get_books_OR.html', context)

# 一中拿多
def get_heros_of_book1(request):
    book1 = BookInfo.objects.get(pk=1)
    herolist = book1.heroinfo_set.all()
    context = RequestContext(request, {'herolist':herolist})
    return render(request, 'booktest/get_heros_of_book1.html', context)

# 多中拿一
def get_book_of_hero1(request):
    hero1 = HeroInfo.objects.get(pk=1)
    book = hero1.book
    context = RequestContext(request, {'book':book})
    return render(request, 'booktest/get_book_of_hero1.html', context)
